package com.steven.ourcontest.team.api

import com.steven.ourcontest.login.helper.LoginController
import com.steven.ourcontest.main.bean.TeamTop5DetailRes
import com.steven.ourcontest.net.ApiTransformer
import com.steven.ourcontest.net.EmptyResponse
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.team.bean.MatchInfoRes
import com.steven.ourcontest.team.bean.TeamApplyRes
import com.steven.ourcontest.team.bean.TeamDetail
import com.steven.ourcontest.team.bean.TeamInfoRes
import io.reactivex.Observable
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class TeamApi private constructor() {

    private val mRetrofit: Retrofit
    private val mApiService: TeamApiService

    companion object {
        const val DEFAULT_TIMEOUT = 5
        val instance: TeamApi by lazy {
            TeamApi()
        }
    }

    init {
        val builder = OkHttpClient.Builder()
        builder.connectTimeout(
            DEFAULT_TIMEOUT.toLong(),
            TimeUnit.SECONDS
        )
        builder.addInterceptor {
            val request = it.request()
            val requestBuilder = request.newBuilder()
            requestBuilder.header("uid", LoginController.userId.toString())
                .header("Accept", "application/json")
                .header("userType", LoginController.userType)
            return@addInterceptor it.proceed(requestBuilder.build())
        }
        mRetrofit = Retrofit.Builder()
            .client(builder.build())
            .baseUrl(NetConstants.CHAT_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
        mApiService = mRetrofit.create(TeamApiService::class.java)
    }

    fun queryUserTeam(user: Long): Observable<TeamInfoRes> {
        return mApiService.queryUserTeam(user).compose(ApiTransformer())
    }

    fun queryTeamDetail(teamId: Long): Observable<TeamDetail> {
        return mApiService.queryTeamDetail(teamId).compose(ApiTransformer())
    }

    fun updateTeamStatus(teamId: Long, status: Boolean): Observable<EmptyResponse> {
        return mApiService.updateTeamStatus(teamId, status).compose(ApiTransformer())
    }

    fun applyToTeam(teamId: Long): Observable<EmptyResponse> {
        return mApiService.applyToTeam(teamId).compose(ApiTransformer())
    }

    fun searchMatchList(matchName: String, lastId: Long): Observable<MatchInfoRes> {
        return mApiService.searchMatchList(matchName, lastId).compose(ApiTransformer())
    }

    fun queryApplyToMe(): Observable<TeamApplyRes> {
        return mApiService.queryApplyToMe().compose(ApiTransformer())
    }

    fun updateApplyToTeam(applyId: Long, pass: Boolean): Observable<EmptyResponse> {
        return mApiService.updateApplyToTeam(applyId, pass).compose(ApiTransformer())
    }

    fun createTeam(teamName: String, matchId: Long, matchName: String, url: String): Observable<EmptyResponse> {
        return mApiService.createTeam(teamName, matchId, matchName, url).compose(ApiTransformer())
    }

    fun addTeamPageViews(teamId: Long): Observable<EmptyResponse> {
        return mApiService.addTeamPageViews(teamId).compose(ApiTransformer())
    }

    fun searchTop5Team(): Observable<TeamTop5DetailRes> {
        return mApiService.searchTop5Team().compose(ApiTransformer())
    }

    fun searchSameSchoolTeam(): Observable<TeamInfoRes> {
        return mApiService.searchSameSchoolTeam().compose(ApiTransformer())
    }

    fun searchNewCreateTeam(lastCreateTime: Long): Observable<TeamInfoRes> {
        return mApiService.searchNewCreateTeam(lastCreateTime).compose(ApiTransformer())
    }
}